@layout("/common/default.html",{title:'字段配置'}){
<style type="text/css">
    /* 防止下拉框下拉值被遮盖*/
    .layui-table-cell {
        overflow: visible;
    }

    .layui-table-box {
        overflow: visible;
    }

   /* .layui-table-body {
        overflow: visible;
    }*/
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">基本信息</div>
    <fieldset class="table-search-fieldset">
        <legend>操作信息</legend>
        <div style="margin: 10px 10px 10px 10px">
            <form class="layui-form layui-form-pane" action="">
                <input type="hidden" id="onlyid" name="id" value="${genTable.id}">
                <input type="hidden" id="op"  value="${genTable.op}">
                <input type="hidden" id="onlyname"  value="${genTable.name}">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">选择数据表</label>
                        <div class="layui-input-inline">
                            <#select path="table" items="${table}" value="${table}" value="${genTable.name!}" lay-verify="true" lay-filter="selectTable" lay-search="true" placeholder="直接选择或搜索选择"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">数据表名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name"  autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">描述</label>
                        <div class="layui-input-inline">
                            <input type="text" id="comments" name="comments"  autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">类名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="className" name="className"  autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline" id="submit" style="display: none">
                        <button type="button" class="layui-btn icon-btn layui-btn-radius" data-type="submit">
                            <i class="layui-icon"></i>保存
                        </button>

                    </div>
                </div>
            </form>
        </div>
    </fieldset>
    <div class="layui-form-item" id="gentableid" style="display: none">
        <table class="layui-table" id="tableId" lay-filter="tableId"  ></table>

    </div>
    </div>
</div>

<form class="layui-form"  lay-filter="layui-form-scheme" id="layui-form-scheme" style="display: none">
    <input type="hidden" id="genTableName" name="genTable.name">
    <input type="hidden"  name="id">
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">生成配置</div>
        <div class="layui-form-item" style="margin-top: 8px;">
            <div class="layui-inline layui-col-md5" >
                <label class="layui-form-label layui-required">方案名称</label>
                <div class="layui-input-block">
                    <input type="text"  id="programme" name="name"    autocomplete="off" class="layui-input" lay-verify="required" >
                </div>
            </div>
            <div class="layui-form-mid layui-word-aux">结构：{包名}/{模块名}/{分层(dao,entity,service,web)}/{子模块名}/{java类}</div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label layui-required">包路径</label>
                <div class="layui-input-block">
                    <input type="text"  id="packageName"   name="packageName"   lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-mid layui-word-aux">建议模块包：com.xxxxx.safe</div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label layui-required">模块名</label>
                <div class="layui-input-block">
                    <input type="text" id="moduleName"   name="moduleName"   lay-verify="required"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-mid layui-word-aux">可理解为子系统名，例如 sys</div>
        </div>

        <div class="layui-form-item" >
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label ">子模块名</label>
                <div class="layui-input-block">
                    <input type="text"  id="subModuleName" name="subModuleName"   lay-verify="" autocomplete="off" class="layui-input">
                </div>
            </div>
                <div class="layui-form-mid layui-word-aux">可选，分层下的文件夹</div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label layui-required">功能描述</label>
                <div class="layui-input-block">
                    <input type="text"  id="functionName" name="functionName"    lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
                <div class="layui-form-mid layui-word-aux">类的描述</div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">生成功能名</label>
                <div class="layui-input-block">
                    <input type="text" id="functionNameSimple" name="functionNameSimple"   autocomplete="off" class="layui-input" >
                </div>
            </div>
                <div class="layui-form-mid layui-word-aux">可选，功能提示</div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label layui-required">开发者</label>
                <div class="layui-input-block">
                    <input type="text" id="functionAuthor" name="functionAuthor"   autocomplete="off" lay-verify="required" class="layui-input" >
                </div>
            </div>
                <div class="layui-form-mid layui-word-aux">功能开发者</div>
        </div>

     </div>
</div>
    <!--<div class="form-group-bottom" style="z-index: 200" align="center">
        <button class="layui-btn" lay-filter="btnSubmit" lay-submit>&emsp;保存&emsp;</button>
        <button class="layui-btn" lay-filter="btnSubmit" lay-submit>&emsp;保存并生成代码&emsp;</button>
        <button   id="btnclose" class="layui-btn layui-btn-primary"  >&emsp;取消&emsp;</button>

    </div>-->
    <div class="form-group-bottom" style="z-index: 200" align="center">
        <button class="layui-btn" lay-filter="submit" lay-submit>保存</button>
        <button class="layui-btn" lay-filter="btnSubmit" lay-submit>&emsp;生成代码&emsp;</button>
        <button   id="btnclose" class="layui-btn layui-btn-primary"  >&emsp;取消&emsp;</button>
    </div>
</form>





<script type="text/html" id="isPk">
    <input type="checkbox" name="isPk" lay-skin="primary" lay-filter="test" {{ d.isPk== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isNull">
    <input type="checkbox" name="isNull" lay-filter="crux" lay-skin="primary" {{ d.isNull== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isInsert">
    <input type="checkbox" name="isInsert" lay-filter="crux" lay-skin="primary" {{ d.isInsert== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isEdit">
    <input type="checkbox" name="isEdit" lay-filter="crux" lay-skin="primary" {{ d.isEdit== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isList">
    <input type="checkbox" name="isList" lay-filter="crux" lay-skin="primary" {{ d.isList== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isQuery">
    <input type="checkbox" name="isQuery" lay-filter="crux" lay-skin="primary" {{ d.isQuery== 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="javaType">
    <#select path="javaType" items="${fnc.getDictList('gen_java_type')}" lay-filter="cruxkey"  value="{{d.queryType}}"/>
</script>
<script type="text/html" id="queryType">
    <#select path="queryType" items="${fnc.getDictList('gen_query_type')}" lay-filter="cruxkey"  value="{{d.queryType}}"/>
</script>
<script type="text/html" id="showType">
    <#select path="showType" items="${fnc.getDictList('gen_show_type')}" lay-filter="cruxkey"  value="{{d.showType}}"/>
</script>


<script>
    layui.use(['element','table', 'form',  'submitform','openLayer', 'resAjax','miniTab','utils'], function () {
        var table = layui.table,
            form = layui.form,
            openLayer = layui.openLayer,
            element = layui.element,
            submitform=layui.submitform,
            miniTab = layui.miniTab,
            resAjax=layui.resAjax;

        //操作类型
        var op=$("#op").val();
        //表的id
        var onlyid=$("#onlyid").val();
        if(op=="add"){
            //监听select操作
            form.on('select(selectTable)', function (obj) {
                var value = obj.value;
                if(value!=''){
                    //表格数据
                    displaytable(value);
                    //查询栏数据
                    getTable(value);
                    //生成配置方案
                    configure();
                    //显示
                    tableShow();
                }else {
                    //数据表名称
                    $("#name").val("");
                    //描述
                    $("#comments").val("");
                    //类名称
                    $("#className").val("");
                    tableHide();
                };

            });
        }else if(op=="edit"){
            var name=$("#onlyname").val();
            //表格数据
            displaytable(name);
            //查询栏数据
            getTable(name);
            //生产配置方案
            configure();
            //显示
            tableShow();
        }

        //显示
        function tableShow() {

            $("#layui-form-scheme").css("display","inline");
            //表格
            $("#gentableid").css("display","inline");
            //保存按钮
            $("#submit").css("display","inline");
        }
        //隐藏
        function tableHide() {
            //表格
            $("#gentableid").css("display","none");
            //保存按钮
            $("#submit").css("display","none");
        }

        //字段管理
        function displaytable(value){
            table.render({
                elem: "#tableId",
                url: "/gen/genTable/tableColumnList?id="+onlyid+"&name="+value,
                height: 315,
                page: true,
                limit: 10,
                cols: [[
                    {field: 'name', title: '列名'}
                    , {field: 'comments', title: '描述', edit: 'text'}
                    , {field: 'jdbcType', title: '物理类型'}
                    , {field: 'javaType', title: 'Java类型', templet: '#javaType'}
                    , {field: 'javaField', title: 'Java属性名称', edit: 'text'}
                    , {field: 'isPk', width: 60, title: '主键', align: 'center', templet: '#isPk'}
                    , {field: 'isNull', width: 60, title: '可空', align: 'center', templet: '#isNull'}
                    , {field: 'isInsert', width: 60, title: '插入', align: 'center', templet: '#isInsert'}
                    , {field: 'isEdit', width: 60, title: '编辑', align: 'center', templet: '#isEdit'}
                    , {field: 'isList', width: 60, title: '列表', align: 'center', templet: '#isList'}
                    , {field: 'isQuery', width: 60, title: '查询', align: 'center', templet: '#isQuery'}
                    , {field: 'queryType', title: '查询匹配方式', templet: '#queryType'}
                    , {field: 'showType', title: '显示表单类型', templet: '#showType'}
                    , {field: 'dictType', title: '字典类型', edit: 'text'}
                    , {field: 'sort', width: 80, title: '排序', edit: 'text'}
                ]],

                done: function (res, curr, count) {
                }
            });
        }

        function getTable(value) {
            //Data数据
            var json={
                name:value,  //表名称
                id:$("#onlyid").val()
            };
            //数据获取
            var result=resAjax.render({
                url:'/gen/genTable/getTable', //url
                type:"get",//类型
                data:json
            });
            if(result.code=="0"){
                //数据表名称
                $("#name").val(result.data.name);
                //描述
                $("#comments").val(result.data.comments);
                //类名称
                $("#className").val(result.data.className);
                //表的名称
                $("#genTableName").val(result.data.name);
            }else {

            }

        }

        //监听checkbox操作
        form.on('checkbox(crux)', function (obj) {
            var elem = $(this)
                , value = obj.elem.checked
                , field = this.name
                , index = elem.parents('tr').eq(0).data('index')
                , data = table.cache["tableId"][index];
            data[field] = value ? 1 : null;
        });

        //监听select操作
        form.on('select(cruxkey)', function (obj) {
            var elem = $(this)
                , value = obj.value
                , field = obj.elem.name
                , index = elem.parents('tr').eq(0).data('index')
                , data = table.cache["tableId"][index];
            data[field] = value;
        });

        //生成方案的配置
        function configure() {
            var name=$("#name").val();
            //数据获取
            var result=resAjax.render({
                url:'/gen/genScheme/getGenScheme', //url
                type:"get",//类型
                data:{
                    "tableName":name
                }
            });
            if(result.data!=null){
                form.val('layui-form-scheme', result.data);
            }else {
                // 重置清空
                $('#layui-form-scheme')[0].reset();
                form.render();//必须写
                form.val('layui-form-scheme', {
                    "packageName":'${fnc.getPackageName()}'
                });
            }

        }


        //生成代码
        form.on("submit(btnSubmit)", function (data) {
            var field=data.field;
            //数据获取
            var result=resAjax.render({
                url:'/gen/genScheme/save', //url
                type:"post",//类型
                data:field
            });
            if(result.code==msgType.success){
                //成功
                openLayer.greenTickMsg(result.msg, function () {
                    close();
                });


            }else if(result.code==msgType.fail){
                //失败
                openLayer.redCrossMsg("生成失败", function () {
                    openLayer.open("错误信息页面",result.msg,'893px', '600px');
                });
            }

            return false; // 阻止提交行为！！
        });

        function close() {
            var iframeTemp = parent.layui.$('iframe[src="/gen/genTable"]');
            // 如果找到了才需要重载，找不到证明没有打开即不需要处理
            if (iframeTemp.length) {
                // 找到这个iframe的window
                iframeTemp = iframeTemp[0].contentWindow;
                // 用他的页面的window的layui去处理他页面的任务（重载表格）
                iframeTemp.layui.table.reload('gentableId', {});
            }
            miniTab.deleteCurrentByIframe();

        }
        //事件
        var active = {
            submit: function () {
                var data = new Object();
                data.listColumn = table.cache["tableId"];
                data.table = {
                    id: $("input[name='id']").val(),
                    name: $("input[name='name']").val(),
                    comments: $("input[name='comments']").val(),
                    className: $("input[name='className']").val()
                };
                var programme=$("#programme").val();
                // 请求后台
                $.ajax({
                    url: '/gen/genTable/save' //实际使用请改成服务端真实接口
                    , type: 'post'
                    , data: JSON.stringify(data)
                    , contentType: 'application/json;charset=UTF-8'
                    , success: function (result) {
                        if(result.code=="0"&&programme==''){
                            configure();
                        }
                       /* layer.alert(result.msg);
                        var iframeTemp = parent.layui.$('iframe[src="/gen/genTable"]');
                        // 如果找到了才需要重载，找不到证明没有打开即不需要处理
                        if (iframeTemp.length) {
                            // 找到这个iframe的window
                            iframeTemp = iframeTemp[0].contentWindow;
                            // 用他的页面的window的layui去处理他页面的任务（重载表格）
                            iframeTemp.layui.table.reload('gentableId', {});
                        }
                        miniTab.deleteCurrentByIframe();*/
                    }, error: function (ex) {
                        layer.alert(ex);
                    }
                });
            },
        };
        $('[data-type]').click(function(){
            var type = $(this).data('type');
            active[type].call(this);
        });

    })
</script>
@}
